package com.sandman.web;

import com.sandman.enums.HttpCodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author 范珈诚
 * time 2:27 PM 9/26/22
 * notes 结果返回类
 */
@Data
public class Result<T> implements Serializable {
    /**
     * 返回代码
     */
    private Integer code;
    /**
     * 错误信息
     */
    private String message;
    /**
     * 传递的文件
     */
    private T data;

    /**
     * 普通结果构造
     */
    private Result(HttpCodeEnum httpCode) {
        code = httpCode.getCode();
        message = httpCode.getMessage();
        data = null;
    }

    /**
     * 成功结果构造
     */
    private Result(Object inputData) {
        code = HttpCodeEnum.SUCCESS.getCode();
        message = HttpCodeEnum.SUCCESS.getMessage();
        data = (T) inputData;
    }

    /**
     * 成功结果构造
     */
    private Result() {
        code = HttpCodeEnum.SUCCESS.getCode();
        message = HttpCodeEnum.SUCCESS.getMessage();
        data = null;
    }

    /**
     * 成功结果
     *
     * @param data 返回的成功对象
     * @return 返回成功结果集
     */
    public static Result sucess(Object data) {
        return new Result(data);
    }

    /**
     * 成功结果
     *
     * @return 返回成功结果集
     */
    public static Result sucess() {
        return new Result();
    }

    /**
     * 错误结果
     *
     * @param errorCode 错误类型
     * @return 返回错误结果对象
     */
    public static Result error(HttpCodeEnum errorCode) {
        return new Result(errorCode);
    }

}
